package com.sali.DFS;


import java.util.ArrayList;
import java.util.List;

/**
 * @author sali
 * @date 2025/5/3 16:36
 */
public class LeetCode_257 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        getRes(res, root, new ArrayList<>());
        return res;
    }

    private void getRes(List<String> res, TreeNode root, List<Integer> path) {
        if (root == null) {
            return;
        }

        path.add(root.val);
        if (root.left == null && root.right == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < path.size() - 1; i++) {
                sb.append(path.get(i) + "->");
            }
            sb.append(path.get(path.size() - 1));
            res.add(sb.toString());
            return;
        }
        if (root.left != null) {
            getRes(res, root.left, path);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            getRes(res, root.right, path);
            path.remove(path.size() - 1);
        }

    }

}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
